package com.ml.mall.service.addr.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ml.mall.consts.CommonConsts;
import com.ml.mall.entity.addr.SysOrderAddr;
import com.ml.mall.entity.company.SysCompanyAddr;
import com.ml.mall.entity.sys.SysUser;
import com.ml.mall.enums.CommonEnum;
import com.ml.mall.exception.BizException;
import com.ml.mall.mapper.addr.SysOrderAddrMapper;
import com.ml.mall.mapper.company.SysCompanyAddrMapper;
import com.ml.mall.security.JwtUtil;
import com.ml.mall.service.addr.SysOrderAddrService;
import com.ml.mall.service.company.SysCompanyAddrService;
import com.ml.mall.service.order.impl.SysOrderServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class SysOrderAddrServiceImpl extends ServiceImpl<SysOrderAddrMapper, SysOrderAddr> implements SysOrderAddrService {
    @Autowired
    private SysOrderAddrMapper sysOrderAddrMapper;

    @Autowired
    private SysCompanyAddrService sysCompanyAddrService;

    @Override
    public List<SysOrderAddr> sysOrderAddrQuery(Long oid) {
        log.info("oid======={}",oid);
        QueryWrapper<SysOrderAddr> wrapper=new QueryWrapper<>();
        wrapper.eq("oid",oid);
        return sysOrderAddrMapper.selectList(wrapper);
    }

    @Override
    public void createOrderAdd(Long oid, Long companyId) {
            QueryWrapper<SysCompanyAddr> sysCompanyAddrQueryWrapper = new QueryWrapper<>();
            sysCompanyAddrQueryWrapper.eq("company_id", companyId);
            List<SysCompanyAddr> sysCompanyAddrList = sysCompanyAddrService.list(sysCompanyAddrQueryWrapper);
            if (null == sysCompanyAddrList || sysCompanyAddrList.size()<=0) {
                throw new BizException(CommonEnum.SELECT_ERROR);
            }
            if(sysCompanyAddrList.size()>0){
                List<SysOrderAddr> sysOrderAddrList = new ArrayList<>();
                for (SysCompanyAddr companyAddr : sysCompanyAddrList) {
                    SysOrderAddr sysOrderAddr = new SysOrderAddr();
                    BeanUtils.copyProperties(companyAddr, sysOrderAddr);
                    sysOrderAddr.setCreateAt(null);
                    sysOrderAddr.setUpdateAt(null);
                    sysOrderAddr.setOid(oid);
                    sysOrderAddr.setId(null);
                    sysOrderAddrList.add(sysOrderAddr);
                }
                log.info("sysOrderAddrList==={}",sysOrderAddrList);
                this.saveBatch(sysOrderAddrList);
            }
    }
}
